/*
* $Id: FieldValuesTable.cpp 55 2010-03-09 22:59:20Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "FieldValuesTable.h"
#include "TableNames.h"
#include "../SqlBinder.h"

CFieldValuesTable::CFieldValuesTable()
: CTable(FIELD_VALUES_TABLE_NAME){}
CFieldValuesTable::~CFieldValuesTable(){}

void CFieldValuesTable::CreateTable(IDatabase* pDatabase){
	std::tstring strRequest(_T("CREATE TABLE IF NOT EXISTS "));
	pDatabase->ExecRequest(strRequest + GetNameTable() + 
							_T("(id_record INTEGER NOT NULL,")\
							_T("name TEXT NOT NULL,")\
							_T("value TEXT NOT NULL,")\
							_T("type INTEGER NOT NULL);"));
}
void CFieldValuesTable::UpdateValue(IDatabase* pDatabase){
	std::tstring strRequest(_T("UPDATE "));
	CSqlBinder binder(strRequest + GetNameTable() + 
					_T(" SET value=? WHERE id_record=? AND name=?"));
	binder.SetValue(2, m_content.s_strName);
	binder.SetValue(1, m_content.s_nId);
	binder.SetValue(0, m_content.s_strValue);
	pDatabase->ExecRequest(binder.GetRequest());
	m_content.Clear();
}
IResult* CFieldValuesTable::GetValue(IDatabase* pDatabase){
	CSqlBinder binder;
	std::tstring strRequest(_T("SELECT * FROM "));
	if(m_content.s_nId != -1){
		binder.SetRequest(strRequest + GetNameTable() + _T(" WHERE id_record=?"));
		binder.SetValue(0, m_content.s_nId);
	}else
		binder.SetRequest(strRequest + GetNameTable());
	m_content.Clear();
	return FillContent(pDatabase->ExecRequest(binder.GetRequest()));
}
void CFieldValuesTable::SetValue(IDatabase* pDatabase){
	CSqlBinder binder;
	std::tstring strRequest(_T("INSERT INTO "));
	binder.SetRequest(strRequest + GetNameTable() + _T("(id_record, name, value, type) VALUES(?, ?, ?, ?)"));
	binder.SetValue(0, m_content.s_nId);
	binder.SetValue(1, m_content.s_strName);
	binder.SetValue(2, m_content.s_strValue);
	binder.SetValue(3, m_content.s_type);
	pDatabase->ExecRequest(binder.GetRequest());
	m_content.Clear();
}
void CFieldValuesTable::DeleteValue(IDatabase* pDatabase){
	CSqlBinder binder;
	std::tstring strRequest(_T("DELETE FROM "));
	if(m_content.s_nId != -1){
		strRequest = strRequest + GetNameTable() + _T(" WHERE id_record=?");
		binder.SetValue(0, m_content.s_nId);
		if(!m_content.s_strName.empty()){
			strRequest = strRequest + _T(" AND name=?");
			binder.SetValue(1, m_content.s_strName);
		}
		binder.SetRequest(strRequest);
	}else{
		binder.SetRequest(strRequest + GetNameTable());
	}
	m_content.Clear();
	pDatabase->ExecRequest(binder.GetRequest());
}
IOperation::i_content* CFieldValuesTable::CreateContent(){
	return new content;
}

int CFieldValuesTable::GetMaxId(IDatabase* pDatabase){
	int nId = 0;
	std::tstring strRequest = _T("SELECT MAX(id_record) as id_record FROM ");
	MyTable table = pDatabase->ExecRequest(strRequest + GetNameTable());
	if(!table.empty())
		nId = _ttoi(table[0][0].m_strValueColumn.c_str());
	return nId;
}

void CFieldValuesTable::content::Clear(){
	s_nId = -1;
	s_strName.clear();
	s_strValue.clear();
	s_type = STRING;
}
void CFieldValuesTable::content::Set(const std::vector<CTableItem>& aContent){
	for(std::vector<CTableItem>::const_iterator it = aContent.begin(); it != aContent.end(); it++){
		if(it->m_strNameColumn == _T("id_record"))
			s_nId = _ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("name"))
			s_strName = it->m_strValueColumn;
		else if(it->m_strNameColumn == _T("type"))
			s_type = (type)_ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("value"))
			s_strValue = it->m_strValueColumn.c_str();
	}
}
void CFieldValuesTable::content::Free(){
	delete this;
}